local jiexing = fk.CreateSkill{
  name = "sd__jiexing",
  frequency = Skill.Compulsory,
}

-- 存储玩家选择的阶段
local player_selected_phases = {}

-- 回合开始时触发
jiexing:addEffect(fk.TurnStart, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(jiexing.name)
  end,
  on_trigger = function(self, event, target, player, data)
    local room = player.room
    
    -- 1. 摸两张牌
    player:drawCards(2, jiexing.name)
    
    -- 2. 选择弃置不同花色的牌并指定阶段
    if player:isAlive() and #player:getCardIds("h") > 0 then
      -- 使用askToUseActiveSkill让玩家选择弃牌
      local success, dat = room:askToUseActiveSkill(player, {
        skill_name = "jiexing_discard",
        prompt = "#jiexing-choose"
      })
      
      if success and dat and dat.cards and #dat.cards > 0 then
        -- 弃置选择的牌
        room:throwCard(dat.cards, jiexing.name, player, player)
        
        -- 记录弃置的牌数
        local discardCount = #dat.cards
        
        -- 清空之前的选择
        player_selected_phases[player.id] = {}
        
        -- 使用askToChoices让玩家选择阶段（只能选择判定、摸牌、出牌、弃牌阶段）
        local available_phases = {
          { name = "判定阶段", value = Player.Judge },
          { name = "摸牌阶段", value = Player.Draw },
          { name = "出牌阶段", value = Player.Play },
          { name = "弃牌阶段", value = Player.Discard }
        }
        
        local choices = {}
        for _, phase_info in ipairs(available_phases) do
          table.insert(choices, phase_info.name)
        end
        
        local prompt = string.format("请选择你要执行的%d个阶段：", discardCount)
        local selected_choices = room:askToChoices(player, {
          choices = choices,
          min_num = discardCount,
          max_num = discardCount,
          prompt = prompt,
          skill_name = jiexing.name,
        })
        
        -- 将选择的阶段名称映射回阶段值
        for _, choice in ipairs(selected_choices) do
          for _, phase_info in ipairs(available_phases) do
            if phase_info.name == choice then
              table.insert(player_selected_phases[player.id], phase_info.value)
              break
            end
          end
        end
        
        -- 直接修改当前回合的phase_table，只保留选择的阶段
        if #player_selected_phases[player.id] > 0 then
          -- data就是TurnData对象，包含phase_table字段
          local new_phase_table = {}
          
          -- 添加开始阶段（总是执行）
          table.insert(new_phase_table, PhaseData:new{
            who = player,
            reason = "game_rule",
            phase = Player.Start
          })
          
          -- 添加用户选择的阶段
          for _, selected_phase in ipairs(player_selected_phases[player.id]) do
            table.insert(new_phase_table, PhaseData:new{
              who = player,
              reason = "sd__jiexing",
              phase = selected_phase
            })
          end
          
          -- 替换原来的phase_table
          data.phase_table = new_phase_table
        end
        
        -- 4. 如果弃置的牌数量大于上一次发动，可以对一名角色造成1点伤害
        local lastCount = player:getMark("@jiexing_last_discard")
        local canCauseDamage = false
        if lastCount > 0 then
          canCauseDamage = discardCount > lastCount
        else
          -- 第一次发动时，只要弃牌就可以造成伤害
          canCauseDamage = discardCount > 0
        end
        
        -- 更新本次弃牌数量
        room:setPlayerMark(player, "@jiexing_last_discard", discardCount)
        
        if canCauseDamage then
          local targets = room:getOtherPlayers(player)
          if #targets > 0 then
            local to = room:askToChoosePlayers(player, {
              targets = targets,
              min_num = 1,
              max_num = 1,
              prompt = "#jiexing-damage",
              skill_name = jiexing.name,
              cancelable = true
            })
            
            if #to > 0 then
              room:damage{
                from = player,
                to = to[1],
                damage = 1,
                skillName = jiexing.name,
              }
            end
          end
        end
      else
        -- 未弃置牌，直接结束回合
        local new_phase_table = {}
        -- 只添加开始阶段和结束阶段
        table.insert(new_phase_table, PhaseData:new{
          who = player,
          reason = "game_rule",
          phase = Player.Start
        })
        table.insert(new_phase_table, PhaseData:new{
          who = player,
          reason = "game_rule",
          phase = Player.Finish
        })
        data.phase_table = new_phase_table
      end
    end
  end,
})

Fk:loadTranslationTable{
  ["sd__jiexing"] = "节行",
  [":sd__jiexing"] = "锁定技，回合开始时，你摸两张牌，然后弃置任意张花色各不相同的牌，然后你选择等量的阶段（准备和结束阶段除外），此回合仅执行这些阶段。若你以此法弃置的牌数量大于上一次发动，则你可以对一名角色造成1点伤害。",
  ["@jiexing_last_discard"] = "节行",

  ["#jiexing-choose"] = "节行：请选择要弃置的牌（花色各不相同）",
  ["#jiexing-damage"] = "节行：你可以对一名角色造成1点伤害（弃牌数量大于上一次）",
  
  ["$sd__jiexing1"] = "",
  ["$sd__jiexing2"] = "",
}

return jiexing
